package co.yixiang.business.attention

import co.yixiang.annotation.AnonymousAccess
import co.yixiang.api.ApiResult
import co.yixiang.business.post.UserInfo
import co.yixiang.entity.Attention
import co.yixiang.entity.AttentionRepository
import co.yixiang.util.UserServiceKT
import co.yixiang.util.getApiResult
import co.yixiang.util.toListVo
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController


interface AttentionService {

    /**
     * 添加关注
     */
    fun insertMyAttention(attention: Attention): ApiResult<*>

    /**
     * 取消关注
     */
    fun removeMyAttention(id: String): ApiResult<*>

    /**
     * 获取我的关注列表接口
     */
    fun getMyAttentionListApi(userId: Long): ApiResult<*>

    /**
     * 获取我的关注列表
     */
    fun getMyAttentionList(userId: Long): MutableList<UserInfo>


    /**
     * 获取我的粉丝列表
     */
    fun getMyFansList(userId: Long): ApiResult<*>

    /**
     * 获取我的粉丝数量
     */
    fun getMyFansCount(userId: Long): Int?
}

@Service
@Transactional(rollbackFor = [Exception::class])
open class AttentionServiceImp(@Autowired val attentionRepository: AttentionRepository,
                               @Autowired val userServiceKT: UserServiceKT): AttentionService {

    override fun insertMyAttention(attention: Attention): ApiResult<*> {

        return attentionRepository.save(attention).block().run { getApiResult(200, "关注成功") }
    }

    override fun removeMyAttention(id: String): ApiResult<*> {

        return attentionRepository.deleteById(id).block().run { getApiResult(200, "取消关注成功") }
    }

    override fun getMyAttentionListApi(userId: Long): ApiResult<*> {

        return getMyAttentionList(userId).run { getApiResult(200, "获取我关注人的列表成功") }
    }

    override fun getMyAttentionList(userId: Long): MutableList<UserInfo> {

        return attentionRepository.getAttentionsByUserIdOrderByCreateTimeDesc(userId).toIterable().toMutableList()
            .toListVo { userServiceKT.getUserInfo(it.attentionUserId!!) }
    }

    override fun getMyFansList(userId: Long): ApiResult<*> {

        return attentionRepository.getAttentionsByAttentionUserIdOrderByCreateTimeDesc(userId).toIterable()
            .toMutableList().takeIf { it.isNotEmpty() }
            ?.run { toListVo { userServiceKT.getUserInfo(it.userId!!) }
                .getApiResult(200, "获取我的粉丝成功") }
            ?: getApiResult(201, "暂无数据")

    }

    override fun getMyFansCount(userId: Long): Int? {

        return attentionRepository.getAttentionsByAttentionUserIdOrderByCreateTimeDesc(userId).toIterable().count()
    }
}

@Api("关注")
@RestController
@RequestMapping("/attention")
class AttentionController(@Autowired val attentionService: AttentionService) {

    @PostMapping("/insertMyAttention")
    @ApiOperation("新增关注")
    @AnonymousAccess
    fun insertMyAttention(attention: Attention): ApiResult<*> = attentionService.insertMyAttention(attention)

    @PostMapping("/removeMyAttention")
    @ApiOperation("取消关注")
    @AnonymousAccess
    fun removeMyAttention(id: String): ApiResult<*> = attentionService.removeMyAttention(id)

    @PostMapping("/getMyAttentionListApi")
    @ApiOperation("获取我关注的列表")
    @AnonymousAccess
    fun getMyAttentionListApi(userId: Long): ApiResult<*> = attentionService.getMyAttentionListApi(userId)

    @PostMapping("/getMyFansList")
    @ApiOperation("获取我的粉丝列表")
    @AnonymousAccess
    fun getMyFansList(userId: Long): ApiResult<*> = attentionService.getMyFansList(userId)
}
